package org.kms.Controllers;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.kms.dao.childDao;
import org.kms.dao.parentsDao;
import org.kms.pojo.Child;
import org.kms.pojo.Parents;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Child")
@CrossOrigin(value = "*")
public class child {
    @Autowired
    private childDao childDao;

    @Autowired
    private parentsDao parentsDao;



    @RequestMapping("/index")
    @ResponseBody
  public ResponseResult<List<Child>> Getall(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Child condition) throws Exception{
        Page<Child> pager = PageHelper.startPage(page,limit);
      List<Child> getlist = childDao.getConditionList(condition);
      return new ResponseResult(getlist,pager.getTotal());
    }

    @RequestMapping("/childAdd")
    @ResponseBody
    public ResponseResult add(Child child,String [] hobby,String [] guanxi,String [] pcName,String [] pCmode)throws Exception{
       StringBuffer sb = new StringBuffer();
        for (int i = 0; i < hobby.length; i++) {
            sb.append(hobby[i]);
            if (i<hobby.length-1){
                sb.append(",");
            }
        }
        child.setHobby(sb.toString());
        if (child.getDpay() == null){
            child.setDpay("否");
        }else {
            child.setDpay("是");
        }
        if (child.getpDrop() == null){
            child.setpDrop("否");
        }else {
            child.setpDrop("是");
        }
        child.setpNation("汉");
        childDao.add(child);
        if (guanxi != null && guanxi.length >0){
            for (int i = 0; i < pcName.length; i++) {
                Parents parents = new Parents();
               parents.setGuanxi(guanxi[i]);
                parents.setpCmode(pCmode[i]);
                parents.setPcName(pcName[i]);
                parents.setPid(child.getPid());
                parentsDao.add(parents);
            }
        }

        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/childDelete")
    @ResponseBody
    public ResponseResult Delete(Integer pid)throws Exception{
        childDao.delete(pid);
        return ResponseResult.createSuccessResult();
    }
    @PostMapping("/childImg")
    @ResponseBody
    public ResponseResult getpersera(MultipartFile file, HttpServletRequest request)throws Exception{
        String path = "F:\\Users\\Administrator\\IdeaProjects\\Kms-Project\\Project\\KindergartenMS\\KindergartenMS-Ui\\src\\main\\webapp\\images\\child\\";
        String childimg = file.getOriginalFilename();
        File file1 = new File(path,childimg);
        file.transferTo(file1);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/childUpdate")
    @ResponseBody
    public ResponseResult Update(Child child,String [] hobby,String [] guanxi,String [] pcName,String [] pCmode)throws Exception{
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < hobby.length; i++) {
            sb.append(hobby[i]);
            if (i<hobby.length-1){
                sb.append(",");
            }
        }
        child.setHobby(sb.toString());
        if (child.getDpay() == null){
            child.setDpay("否");
        }else {
            child.setDpay("是");
        }
        if (child.getpDrop() == null){
            child.setpDrop("否");
        }else {
            child.setpDrop("是");
        }
        child.setpNation("汉");
        childDao.update(child);
        if (guanxi != null && guanxi.length >0){
            for (int i = 0; i < pcName.length; i++) {
                Parents parents = new Parents();
                parents.setGuanxi(guanxi[i]);
                parents.setpCmode(pCmode[i]);
                parents.setPcName(pcName[i]);
                parents.setPid(child.getPid());
                parentsDao.update(parents);
            }
        }

        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/childNum")
    @ResponseBody
    public int getbychildNum(Integer glId,Integer cmId)throws Exception{
        return childDao.getbymanageNum(glId,cmId);
    }
    @InitBinder
    public void InitBinder(WebDataBinder webDataBinder) throws Exception{
        //日期类型转换器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat,true);
        //注册类型换换器:如果碰到Date.class 都会转成yyyy-MM-dd格式
        webDataBinder.registerCustomEditor(Date.class,dateEditor);
    }
}
